using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Net.Sockets;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using NetRosApp.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI.EventBroker;
using NetRosApp.Infrastructure.Interface.Constants;
using Microsoft.Practices.CompositeUI.WinForms;
using DevExpress.XtraEditors;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using System.Windows.Forms;
using NetRosApp.Infrastructure.Interface.Services;
using System.Drawing;

namespace NetRosApp.Infrastructure.Interface.Base.BaseViews
{
    

    //public class BaseEditWorkItemStarterController : WorkItemController
    //{
    //    [EventSubscription(EventTopicNames.OnShowEditorByType, ThreadOption.UserInterface)]
    //    public void OnShowEditorByType(object sender, EventArgs<object, Type> args)
    //    {
    //        Type test = args.Data1;
            
            
    //    //    BaseEditWorkItemController<XtraUserControl> tmp;
    //       // ControlledWorkItem<test> temp = new ControlledWorkItem<test>();

    //        //ControlledWorkItem<BaseEditWorkItemController<> wrptv = WorkItem.WorkItems.Get(args.Data1) as ControlledWorkItem<ReportViewWorkItemController>;

    //        //if (wrptv == null)
    //        //{
    //        //    wrptv = WorkItem.WorkItems.AddNew<ControlledWorkItem<BaseEditWorkItemController<EditorToOpen>>>(args.Data1);
    //        //    wrptv.Controller.WorkItem.Items.Add(args.Data, "Report");
    //        //    wrptv.Controller.WorkItem.Items.Add(args.Data1, "Key");
    //        //}

    //        //wrptv.Controller.Run();

    //    }
    //}
      
    public class BaseEditWorkItem : WorkItem 
    {
        [EventPublication(EventTopicNames.EntityItemChanged, PublicationScope.Descendants)]
        public event EventHandler<EventArgs<object,string>> EntityItemChanged;
    
        Type _tip;
        object _view;

        public BaseEditWorkItem(Type View)
        {
            _tip = View;
        }
  

             public void Start(object Entity, object parametri)
        {

                 
                try
                {
                    _view = SmartParts.Get(_tip.FullName);

                    if (_view == null)
                    {
                        _view = SmartParts.AddNew(_tip, _tip.FullName);
                    }


                    Workspaces[WorkspaceNames.RightWorkspace].Show(_view);
                    //   (_view as Control).Focus();
                    if (Entity == null && parametri != null && parametri as String != "Entity" &&
                        parametri as string == null)
                    {
                        EventArgs<object, object> tosend = new EventArgs<object, object>(Entity, parametri);
                        OnEntityItemChanged(new EventArgs<object, string>(tosend, "Entity"));
                        return;
                    }

                    if (Entity != null)
                    {
                        if (parametri != null && parametri as String != "Entity")
                        {
                            OnEntityItemChanged(new EventArgs<object, string>(Entity, parametri as string));
                        }
                        else
                        {
                            this.Items.Add(Entity, "Entity");
                            OnEntityItemChanged(new EventArgs<object, string>(Entity, "Entity"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExceptionPolicy.HandleException(ex, "MsgBox");
                }
                //}
                //else
                //{
                //    if (Entity != Item)
                //    {
                //        this.Items.Remove(Item);
                //        this.Items.Add(Entity, "Entity");
                //        OnEntityItemChanged(new EventArgs<object>(Entity));
                //    }
                //}
           
           
        }

        public void StartModal(object Entity, WindowSmartPartInfo SmartPartinfo, object parametri)
        {
            try
            {
                // {
                _view = SmartParts.AddNew(_tip, _tip.FullName);
                //}

                if (Entity != null)
                {
                    object tosend = Entity;
                    //       this.Items.Add(Entity, "Entity");
                    if (parametri != null)
                    {
                        tosend = new EventArgs<object, object>(Entity, parametri);
                    }
                    OnEntityItemChanged(new EventArgs<object, string>(tosend, "Entity"));
                }

                if (SmartPartinfo == null)
                {
                }
                else
                {
                    SmartPartinfo.ControlBox = false;
                    bool title = false;
                    if (_view as BaseTraziView != null)
                    {
                        BaseTraziView _viewb = _view as BaseTraziView;
                        SmartPartinfo.Title = (_view as BaseTraziView).Title;
                        title = true;
                        if (Workspaces[WorkspaceNames.RightWorkspace].SmartParts.Count != 0)
                        {
                            Control _loc = Workspaces[WorkspaceNames.RightWorkspace].SmartParts[0] as Control;
                            if (_loc != null)
                            {
                                Rectangle _rec = System.Windows.Forms.Screen.FromControl(_loc).WorkingArea;
                                if (((_rec.Height) < _viewb.Height) || ((_rec.Width) < _viewb.Width))
                                {

                                    _viewb.Height = (_rec.Height -100);
                                    _viewb.Width= (_rec.Width-20);
                                }
                            }
                        }
                    }
                    if (_view as BaseEditorView != null)
                    {
                        BaseEditorView _viewb = _view as BaseEditorView;
                        SmartPartinfo.Title = (_view as BaseEditorView).Title;
                        title = true;
                        if (Workspaces[WorkspaceNames.RightWorkspace].SmartParts.Count != 0)
                        {
                            Control _loc = Workspaces[WorkspaceNames.RightWorkspace].SmartParts[0] as Control;
                            if (_loc != null)
                            {
                                
                                    Rectangle _rec = System.Windows.Forms.Screen.FromControl(_loc).WorkingArea;
                                    if (((_rec.Height) < _viewb.Height) || ((_rec.Width) < _viewb.Width))
                                    {
                                       _viewb.Height = (_rec.Height-100);
                                        _viewb.Width = (_rec.Width-20);
                                    }
                            }
                        }
                    }
                    if (!title)
                    {
                        String test = _view.GetType().ToString();
                        SmartPartinfo.Title = test.Replace("NetRosApp", "NApp");
                    }
                    
                    
                    Workspaces[WorkspaceNames.ModalWindows].Show(_view, SmartPartinfo);
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, "MsgBox");
            }


            //}
            //else
            //{
            //    if (Entity != Item)
            //    {
            //        this.Items.Remove(Item);
            //        this.Items.Add(Entity, "Entity");
            //        OnEntityItemChanged(new EventArgs<object>(Entity));
            //    }
            //}

          //  _view = SmartParts.Get(_tip.GetType().ToString());

            //if (_view == null)
      


         
           

        }

        protected virtual void OnEntityItemChanged(EventArgs<object,string> eventArgs)
        {
            if (EntityItemChanged != null)
            {
                EntityItemChanged(this, eventArgs);
            }
        }
        
    }
}
